<template>
  <div>
    <Card>
      <tables
        ref="tables"
        editable
        searchable
        search-place="bottom"
        v-model="list"
        :columns="columns"
      />
      <Button style="margin: 10px 0" type="primary" @click="exportExcel"
        >导出为Csv文件</Button
      >
    </Card>
  </div>
</template>

<script>
import { getUserComms, delComm } from '@/api/user'
import Tables from '_c/tables'
export default {
  name: 'commit',
  components: {
    Tables
  },
  data () {
    return {
      tableData: [],
      pagenum: 1,
      pageSize: 10,
      pagetotal: 9,
      historyData: [],
      list: [],
      id: [],
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center',
          fixed: 'left',
          _checked: true,
        },
        {
          title: '评论内容',
          key: 'content',
          width: 500
        },
        {
          title: '发表时间',
          key: 'created',
          sortable: true,
          width: 200
        },
        {
          title: '用户',
          key: 'cuid',
          width: 200,
          render: (h, params) => {
            let text = ''
            for (var i in params.row.cuid) {
              text = params.row.cuid.name
            }
            return h("div", {
              props: {
              }
            }, text)
          }
        },
        {
          title: '点赞数量',
          key: 'isRead',
          width: 100

        },
        // {
        //   title: '帖子状态',
        //   align: 'center',
        //   slot: 'post',
        //   width: 200

        // },
        {
          title: '帖子状态',
          width: 200,
          render: (h, params) => {
            let show = ''
            for (var i in params.row.tid) {
              if (params.row.tid.status === '1') {
                show = '异常'
              } else {
                show = '正常'
              }
            }
            return h("div", {
              props: {
              }
            }, show)
          }
        },
        {
          title: '用户状态',
          width: 200,
          render: (h, params) => {
            let show = ''
            for (var i in params.row.cuid) {
              if (params.row.cuid.status === '1') {
                show = '异常'
              } else {
                show = '正常'
              }
            }
            return h("div", {
              props: {
              }
            }, show)
          }
        },
        {
          title: '是否显示',
          key: 'isShow',
          width: 200,
          render: (h, params) => {
            let show = ''
            if (params.row.isShow === '1') {
              show = '是'
            } else {
              show = '否'
            }
            return h("div", {
              props: {}
            }, show)
          }
        },
        {
          title: '是否已读',
          key: 'isRead',
          width: 200,
          render: (h, params) => {
            let read = ''
            if (params.row.isRead === '1') {
              read = '已读'
            } else {
              read = '未读'
            }
            return h("div", {
              props: {}
            }, read)
          }

        },
        {
          title: 'Action',
          key: 'action',
          fixed: 'right',
          width: 120,
          render: (h, params) => {
            let icon = 'md-eye-off'
            let size = '20'
            if (params.row.isBest == 0) {
              return h('div', [
                h('icon', {
                  props: {
                    type: 'md-construct',
                    size: 20,
                  }
                }, 'sz'),
                h('icon', {
                  props: {
                    type: icon,
                    size: size
                  },
                  on: {
                    click: () => {
                      // this.remove(params.index)
                      //变成1
                      params.row.isBest === 1
                    }
                  }
                }, 'del'),
                h('icon', {
                  props: {
                    type: 'ios-trash',
                    size: 20
                  },
                  on: {
                    click: () => {
                      this.remove(params.index)
                      this.remove(params.row)
                    }
                  }
                }, 'del')
              ]);
            } else {
              return h('div', [
                h('icon', {
                  props: {
                    type: 'md-construct',
                    size: 20,
                  }
                }, 'sz'),
                h('icon', {
                  props: {
                    type: 'ios-trash',
                    size: 20
                  },
                  on: {
                    click: () => {
                      this.remove(params.index)
                    }
                  }
                }, 'del')
              ])
            };
          }
        }
      ]
    };
  },
  methods: {
    changepage (index) {
      let self = this
      let _start = (index - 1) * this.pageSize;
      let _end = index * this.pageSize;
      this.historyData = this.list.slice(_start, _end)
    },
    del () {
      //取isselected数据删除
      let ids = ""
      this.id.forEach((e) => {
        ids += e._id + ","
      });
      ids = ids.substring(0, ids.length - 1)
      //请求
      delComm(ids).then(res => {
        if (res.code === 200) {
          this.getComments()
        }
      })
    },
    isselectd (selection) {
      this.id = selection
    },
    remove (row, index) {
      this.historyData.splice(index, 1)
      let ids = row._id
      delComm(ids).then(res => {
        if (res.code === 200) {
          this.$alert('删除成功')
        }
      })
    },
    noeye (index) {
      //对数进行操作
      console.log('aa');
      // this.data1.splice(index, !isShow)


      //换图标
    },
    getComments () {
      getUserComms().then(res => {
        if (res.code === 200) {
          this.list = res.data
          this.pagetotal = res.length
          if (this.list < this.pageSize) {
            this.historyData = this.list
          } else {
            this.historyData = this.list.slice(0, this.pageSize);
            console.log(this.historyData);
          }

        }
      })
    },
    handleSelectAll (index) {
      console.log(index);
    },
    exportExcel () {
      this.$refs.tables.exportCsv({
        filename: `table-${(new Date()).valueOf()}.csv`
      })
    }
  },
  mounted () {
    this.getComments()

  }
}
</script>

<style lang='scss'>
</style>
